package com.albert.controller;

import com.albert.entity.User;
import com.albert.service.UserService;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }


    @GetMapping("/string")
    @SentinelResource(value = "getSkuResource",blockHandler = "blockHandler",fallback = "getStringFallback")
    public String getString(String name){
        try(Entry resource = SphU.entry("protectResource")){
            System.out.println("受保护的资源");
        }catch (BlockException e){
            log.info("资源被限制");
        }
        return "字符串"+name;
    }

    public String blockHandler(String name,BlockException e){
        log.info("方法被限流");
        return null;
    }

}
